package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NavDaoImpl implements INavDao {
    @Override
    public int addNav(NavModel navModel) {
        String sql = "insert into nav (title,href,create_time) values('"+ navModel.getTitle() +"','"+ navModel.getHref()+"','"+ navModel.getCreateTime() +"')";
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteNav(int id) {
        String sql = "delete from nav where id="+id;
        return JDBCUtils.excute(sql);
    }

    @Override
    public int editNav(NavModel navModel) {
        String sql = "update nav set title='"+ navModel.getTitle() +"',href='"
                + navModel.getHref() +"',update_time='"+ navModel.getUpdateTime()+
                "' where id="+navModel.getId();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NavModel> getNavList() {
        String sql = "select * from nav";
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<NavModel> list = new ArrayList<>();
        try {
            while (rs.next()){
                NavModel navModel = new NavModel();
                navModel.setId(rs.getInt("id"));
                navModel.setTitle(rs.getString("title"));
                navModel.setHref(rs.getString("href"));
                list.add(navModel);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public NavModel getNav(int id) {
        String sql = "select * from nav where id="+id;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NavModel navModel = null;
        try {
            while (rs.next()){
                navModel = new NavModel();
                navModel.setId(rs.getInt("id"));
                navModel.setTitle(rs.getString("title"));
                navModel.setHref(rs.getString("href"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return navModel;
    }

    @Override
    public List<NavModel> getNavList(int page, int size) {
        String sql = "select * from nav limit "+ (page-1)*size +","+size;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<NavModel> list = new ArrayList<>();
        try {
            while (rs.next()){
                NavModel navModel = new NavModel();
                navModel.setId(rs.getInt("id"));
                navModel.setTitle(rs.getString("title"));
                navModel.setHref(rs.getString("href"));
                list.add(navModel);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NavModel> getNavList(int page, int size, String sort) {
        String sql = "select * from nav order by "+ sort +" limit "+ (page-1)*size +","+size;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<NavModel> list = new ArrayList<>();
        try {
            while (rs.next()){
                NavModel navModel = new NavModel();
                navModel.setId(rs.getInt("id"));
                navModel.setTitle(rs.getString("title"));
                navModel.setHref(rs.getString("href"));
                list.add(navModel);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NavModel> searchNav(String sql) {
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<NavModel> list = new ArrayList<>();
        try {
            while (rs.next()){
                NavModel navModel = new NavModel();
                navModel.setId(rs.getInt("id"));
                navModel.setTitle(rs.getString("title"));
                navModel.setHref(rs.getString("href"));
                list.add(navModel);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public long getNavCount() {
        String sql = "select count(*) from nav";
        return JDBCUtils.getCount(sql);
    }
}
